package com.ivr.cm.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;

import com.ivr.cm.cache.impl.ConfigFilterCache;
import com.ivr.cm.data.BaseData;
import com.ivr.cm.vo.ConfigFilterVo;

/**
 * 呼入过滤业务
 * @author liugeng
 * @date 2015-12-16
 *
 */
public class FilterServiceImpl {
	private static Logger logger = Logger.getLogger(FilterServiceImpl.class);
	
	/**
	 * 呼入过滤
	 * @param baseData
	 * @return
	 */
	public static int callinFilter(BaseData baseData){
		try{
			List<ConfigFilterVo> configFilterVoList = ConfigFilterCache.CONFIGFILTERMAP.get(baseData.getLong_called());
			int result = 0;
			if(configFilterVoList != null && configFilterVoList.size() > 0){
				for(ConfigFilterVo configFilterVo : configFilterVoList){
					if("".equals(configFilterVo.getProvince()) || baseData.getProvince().equals(configFilterVo.getProvince())){
						result = doFilter(configFilterVo);
						break;
					}
					result = 1;
					continue;
				}
			}
			
			if(result != -1){
				String text = String.format("[%s]使用[%s]业务无过滤设置", baseData.getCalling(),baseData.getLong_called());
				if(result == 1){
					text = String.format("[%s]使用[%s]业务在[%s]不参与过滤", baseData.getCalling(),baseData.getLong_called(),baseData.getProvince());
				}else if(result == 2){
					text = String.format("[%s]使用[%s]业务在[%s]参与过滤,过滤时间外", baseData.getCalling(),baseData.getLong_called(),baseData.getProvince());
				}else if(result == 3){
					text = String.format("[%s]使用[%s]业务在[%s]参与过滤,在过滤时间内,未被选中", baseData.getCalling(),baseData.getLong_called(),baseData.getProvince());
				}
				
				logger.info(String.format("[%s][过滤]未过滤,原因:%s", baseData.getCalling(),text));
				return 0;
			}else{
				logger.info(String.format("[%s]使用[%s]业务在[%s]被过滤", baseData.getCalling(), baseData.getLong_called(),baseData.getProvince()));
				return 1;
			}
		}catch (Exception e) {
			logger.error(String.format("呼入过滤异常信息:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		return 0;
	}
	
	private static int doFilter(ConfigFilterVo configFilterVo){
		boolean isInTime = isInTime(configFilterVo);
		if(!isInTime){
			//该省份参与过滤,过滤时间外
			return 2;
		}
		
		/** 不再根据随机数的方式进行扣量的计算，采用数组匹配的方式进行保证准确扣量 */
		List<Integer> thres_hold = configFilterVo.getThres_hold();
		int thres_index = configFilterVo.getThres_index() + 1;
		configFilterVo.setThres_index(thres_index);
		logger.info(String.format("长号码[%s],过滤拨打序列值[%d]", configFilterVo.getSp_number(), configFilterVo.getThres_index()));
		for (int i = 0; i < thres_hold.size(); i ++) {
			if(thres_hold.get(i) == thres_index) {
				logger.info(String.format("长号码[%s],过滤命中值扣量命中数为[%d]", configFilterVo.getSp_number(), configFilterVo.getThres_index()));
				return -1;
			}
		}
		
		logger.info(String.format("长号码[%s],过滤时间内，该省份参与过滤，未被选中!", configFilterVo.getSp_number()));
		return 3;
	}
	
	private static boolean isInTime(ConfigFilterVo configFilterVo){
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("HH:mm:ss");
		String current = df.format(date);
		String start = configFilterVo.getStart_time() + ":00";
		String end = configFilterVo.getEnd_time() + ":59";
		if (start.compareTo(end) < 0) { 
			// 开始时间早于结束时间
			return (current.compareTo(start) >= 0 && current.compareTo(end) <= 0);
		} else { 
			// 开始时间晚于结束时间（凌晨）
			return (current.compareTo(start) >=0 || current.compareTo(end) <= 0);
		}
	}
	
}